<template>
  <div class="box1">
    <div class="login_form">
      <div class="logo">
        <img src="../assets/logo.png" alt="">
      </div>
      <el-form ref="loginFormRef" :model="loginForm" label-width="0px" :rules="rules" class="form_input">
        <el-form-item prop="username">
          <el-input placeholder="请输入用户名" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入密码" v-model="loginForm.password" type="password" ></el-input>
        </el-form-item>
        <div class="btns">
          <el-button type="success" @click="login">登录</el-button>
          <el-button type="warning" @click="resetForm">重置</el-button>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
  export default {
    data(){
      return {
        loginForm:{
          username:'lisi',
          password:'123456'
        },
        rules:{
          username:[
            { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ],
          password:[
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 6, max: 18, message: '长度在 6 到 18 个字符', trigger: 'blur' }
          ]
        }
      }
    },
    methods:{
      login(){
        this.$refs.loginFormRef.validate( async (valid)=>{
          if(!valid) return
          const { data : res } = await this.$http.post('login',this.loginForm)
          // console.log(res)
          //{data: null, meta: {…}}
          // data: null
          // meta: {msg: "参数错误", status: 400}
          // __proto__: Object
          if(res.meta.status !== 200){
            return this.$message({
              message:'登录失败',
              type:'error'
            })
          }
          this.$message.success('登录成功')
          window.sessionStorage.setItem('token',res.data.token)
          this.$router.push('/home')
        })
      },
      resetForm () {
        this.$refs.loginFormRef.resetFields();//使用refs.则可以自我重新命名ref内的属性
        // this.$refs[loginFormRef].resetFields();//使用refs[]则不能重新命名ref内的属性，需在已有的data内获取
      }
    }
  }
</script>
<style lang="less">
  .box1{
    background: #2b4b6b;
    height: 100%
  }
  .login_form{
    border-radius: 7px;
    background: #fff;
    width: 400px;
    height: 270px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform:translate(-50%,-50%);
  }
  .logo{
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    border-radius: 50%;
    background: #fff;
    box-shadow:0 0 10px #ddd;
    width: 100px;
    height: 100px;
    padding: 10px;
    img{
       width: 100%;
       height: 100%;
       border-radius:50%;
    }
   }
  .form_input{
    width: 100%;
    position: absolute;
    bottom: 0;
    padding: 20px;
    box-sizing: border-box;
  }
  .btns{
    width: 100%;
    display: flex;
    justify-content: flex-end;
  }
</style>
